/**
 * 
 */
package fr.gfi.agraf.persistence.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.NoResultException;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import fr.gfi.agraf.persistence.dao.IActiviteDAO;
import fr.gfi.agraf.persistence.entity.Activite;
import fr.gfi.agraf.utils.PropertiesUtils;

/**
 * @author Zakaria
 * 
 */
@Repository
public class ActiviteDAO extends AbstractGenericDAO<Activite> implements IActiviteDAO {

	private static final String HQL_ACTIVITE_SELECT_BY_PERIMETRE_AND_UTILISATEUR = "hql.activite.selectByPerimetreAndUtilisateur";
	private static final String ID_UTILISATEUR = "idUtilisateur";
	private static final String ID_PERIMETRE = "idPerimetre";
	private static final String ERREUR_AUCUNE_ACTIVITES_TROUVEES = "erreur.activite.notfound";
	private static final String HQL_ACTIVITE_SELECT_BY_PERIMETRE = "hql.activite.selectByPerimetre";

	/**
	 * Logger
	 */
	private final static Logger LOGGER = Logger.getLogger(ComposantDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.gfi.agraf.persistence.dao.IActiviteDAO#
	 * getListeActivitesByPerimetreAndUtilisateur(java.lang.Long,
	 * java.lang.Long)
	 */
	public List<Activite> getListeActivitesByPerimetreAndUtilisateur(final Long idPerimetre,
			final Long idUtilisateur) {
		List<Activite> output = null;
		String query = null;
		try {
			if (idUtilisateur == null) {
				query = PropertiesUtils.getProperty(HQL_ACTIVITE_SELECT_BY_PERIMETRE);
				output = em.createQuery(query, Activite.class).setParameter(ID_PERIMETRE, idPerimetre)
						.getResultList();
			} else {
				query = PropertiesUtils.getProperty(HQL_ACTIVITE_SELECT_BY_PERIMETRE_AND_UTILISATEUR);
				output = em.createQuery(query, Activite.class).setParameter(ID_PERIMETRE, idPerimetre)
						.setParameter(ID_UTILISATEUR, idUtilisateur).getResultList();
			}
		} catch (final NoResultException e) {
			LOGGER.error(ERREUR_AUCUNE_ACTIVITES_TROUVEES, e);
			// Je renvoie une liste vide
			output = new ArrayList<Activite>();
		}

		return output;
	}

}
